package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"mime/multipart"
)

func UploadFile(file *multipart.FileHeader, name string) (string, error) {

	accessKey := "Hd-qhBEj5aFO2WpbDnR5a8JhXWbIftCH-6oLPEwD"
	secretKey := "2UUYWWAjyN0HtYSyotnK4H1PVDaYF_eDQp4QOsrY"
	mac := credentials.NewCredentials(accessKey, secretKey)

	bucket := "lfs3"
	key := name

	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	open, err := file.Open()
	if err != nil {
		return "", err
	}
	open.Close()
	err = uploadManager.UploadReader(context.Background(), open, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		FileName:   key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	if err != nil {
		return "", err
	}
	return "sxd2xg1ty.hd-bkt.clouddn.com" + key, err
}
